#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
    string compressString(string S) {
        string str;
        char pre = '\0';
        int count = 0;
        for (const auto& ch : S)
        {
            if (pre != ch)
            {
                if (count > 0)
                {
                    string num = to_string(count);
                    str += num;
                }
                pre = ch;
                str += pre;
                count = 1;
            }
            else
                count++;
        }
        if (count > 0)
        {
            string num = to_string(count);
            str += num;
        }
        if (str.size() >= S.size())
            return S;
        return str;
    }
};